/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentation;

import Presentation.Model.CriteriaTableModel;
import DataAccess.entity.Criteria;
import DataAccess.entity.DateCriteria;
import DataAccess.entity.ListProperty;
import DataAccess.entity.Property;
import DataAccess.session.ClientefornitoreFacade;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Domenico
 */
public class ParametriRicercaClienteForm extends javax.swing.JFrame {
    private ClientefornitoreFacade CF_facade;
    private String clienteFornitore;
    private ArrayList<Property> listaCampi = new ArrayList<Property>();
    /**
     * Creates new form ParametriRicerca
     */
    public ParametriRicercaClienteForm() {
        initComponents();
    }
    public ParametriRicercaClienteForm(String C_F, ClientefornitoreFacade CFFacade) {
        initComponents();
        clienteFornitore = C_F;
        CF_facade = CFFacade;
        
        listaCampi = CF_facade.getAvancedResearchFields(clienteFornitore);
        jComboBox1.removeAllItems();
        jComboBox2.removeAllItems();
        jComboBox3.removeAllItems();
        popolaComboboxProperty();
        jTable1.setModel(new CriteriaTableModel(new ArrayList<Criteria>()));
        
    }
    public ParametriRicercaClienteForm(String C_F, ClientefornitoreFacade CFFacade, ArrayList<Criteria> lista) {
        initComponents();
        clienteFornitore = C_F;
        CF_facade = CFFacade;
        
        listaCampi = CF_facade.getAvancedResearchFields(clienteFornitore);
        jComboBox1.removeAllItems();
        jComboBox2.removeAllItems();
        jComboBox3.removeAllItems();
        popolaComboboxProperty();
        jTable1.setModel(new CriteriaTableModel(lista));
        
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jXDatePicker1 = new org.jdesktop.swingx.JXDatePicker();
        jComboBox3 = new javax.swing.JComboBox();

        jLabel1.setText("Proprietà:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jLabel2.setText("Condizione:");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setText("Valore:");

        jTextField1.setText("jTextField1");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Aggiungi Condizione");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Rimuovi Condizione");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Ok");

        jButton4.setText("Annulla");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton1)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addGap(24, 24, 24)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jComboBox2, 0, 240, Short.MAX_VALUE)
                                    .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jTextField1)
                                    .addComponent(jXDatePicker1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jComboBox3, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jXDatePicker1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(33, 33, 33)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (controllaCriterio()) {
            creaCriterio();
        } else {
            JOptionPane.showMessageDialog(this,
            "Inserire valore del criterio di ricerca",
            "Valore non Inserito", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        popolaCondizioni_Valori();
        
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       rimuoviCriterio(jTable1.getSelectedRow());
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ParametriRicercaClienteForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ParametriRicercaClienteForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ParametriRicercaClienteForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ParametriRicercaClienteForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ParametriRicercaClienteForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JComboBox jComboBox3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private org.jdesktop.swingx.JXDatePicker jXDatePicker1;
    // End of variables declaration//GEN-END:variables

    private void popolaComboboxProperty() {
        
        for (int i = 1; i < listaCampi.size(); i++) {
            jComboBox1.addItem(listaCampi.get(i).getFieldAlias());
        }
        popolaCondizioni_Valori();
    }

    private void popolaCondizioni_Valori() {
        int indice = jComboBox1.getSelectedIndex() + 1;
        Property p = listaCampi.get(indice);
        jComboBox2.removeAllItems();
        jComboBox3.removeAllItems();
        if (p.getFieldType().equals("LIST")) {
            jComboBox2.addItem("UGUALE A");
            jComboBox2.addItem("DIVERSO DA");
            
            ListProperty lp = (ListProperty) p;
            jTextField1.setText("");
            jTextField1.setEnabled(false);
            
            jComboBox3.setEnabled(true);
            for (int i = 0; i < lp.getValues().size(); i++) {
                jComboBox3.addItem(lp.getValues().get(i));
            }
        }
        if (p.getFieldType().equals("INT")) {
            jComboBox2.addItem("UGUALE A");
            jComboBox2.addItem("DIVERSO DA");
            jComboBox2.addItem("MAGGIORE DI");
            jComboBox2.addItem("MINORE DI");
            jComboBox2.addItem("MAGGIORE UGUALE DI");
            jComboBox2.addItem("MINORE UGUALE DI");
            
            jComboBox3.removeAllItems();
            jComboBox3.setEnabled(false);
            jXDatePicker1.setDate(null);
            jXDatePicker1.setEnabled(false);
            jTextField1.setText("");
            jTextField1.setEnabled(true);
        }
        if (p.getFieldType().equals("VARCHAR")) {
            jComboBox2.addItem("UGUALE A");
            jComboBox2.addItem("DIVERSO DA");
            jComboBox2.addItem("CONTIENE");
            jComboBox2.addItem("NON CONTIENE");
            
            jComboBox3.removeAllItems();
            jComboBox3.setEnabled(false);
            jXDatePicker1.setDate(null);
            jXDatePicker1.setEnabled(false);
            jTextField1.setEnabled(true);
            jTextField1.setText("");
        }
        if (p.getFieldType().equals("DATE")) {
            jComboBox2.addItem("UGUALE A");
            jComboBox2.addItem("DIVERSO DA");
            jComboBox2.addItem("MAGGIORE DI");
            jComboBox2.addItem("MINORE DI");
            jComboBox2.addItem("MAGGIORE UGUALE DI");
            jComboBox2.addItem("MINORE UGUALE DI");
            
            jComboBox3.removeAllItems();
            jComboBox3.setEnabled(false);
            jXDatePicker1.setDate(null);
            jXDatePicker1.setEnabled(true);
            jTextField1.setEnabled(false);
            jTextField1.setText("");
        }
        
    }

    private boolean controllaCriterio() {
        int indice = jComboBox1.getSelectedIndex() + 1;
        Property p = listaCampi.get(indice);
        
        if (p.getFieldType().equals("LIST")) {
            return true;
        }
        if (p.getFieldType().equals("INT")) {
            if (jTextField1.getText().isEmpty()) return false;
            Integer i = Integer.getInteger(jTextField1.getText());
            Double d = null;
            if (i == null) {
                d = Double.parseDouble(jTextField1.getText());
            } else {
                return true;
            }
            return (d != null);
        }
        if (p.getFieldType().equals("VARCHAR")) {
            
            if (jTextField1.getText().isEmpty()) return false;
            return true;
        }
        if (p.getFieldType().equals("DATE")) {
            
            if (jXDatePicker1.getDate() == null) {
                return false;
                
            }
            return true;
        }
        return false;
    }

    private void creaCriterio() {
        int indice = jComboBox1.getSelectedIndex() + 1;
        Property p = listaCampi.get(indice);
        String c = jComboBox2.getSelectedItem().toString();
        String v = "";
        Criteria cri = null;
        if (p.getFieldType().equals("LIST")) {
            v = jComboBox3.getSelectedItem().toString();
            cri = new Criteria(p, c, v);
        }
        if (p.getFieldType().equals("INT")) {
            v = jTextField1.getText();
            cri = new Criteria(p, c, v);
        }
        if (p.getFieldType().equals("VARCHAR")) {
            v = jTextField1.getText();
            cri = new Criteria(p, c, v);
        }
        if (p.getFieldType().equals("DATE")) {
            v = jXDatePicker1.getDate().toString();
            cri = new DateCriteria(p, c, v);
        }
        
        CriteriaTableModel ctm;
        ctm = (CriteriaTableModel) jTable1.getModel();
        ctm.addRow(cri);
    }

    private void rimuoviCriterio(int selectedRow) {
        if (selectedRow > -1) {
            CriteriaTableModel ctm;
            ctm = (CriteriaTableModel) jTable1.getModel();
            ctm.removeRow(selectedRow);
        }
        else {
             JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),
                    "Selezionare una riga",
                    "Errore", JOptionPane.WARNING_MESSAGE);
            return;
        }
    }

    public ArrayList<Criteria> getCriteria() {
        CriteriaTableModel m = (CriteriaTableModel) jTable1.getModel();
        return m.getCriteria();
    }
    
           

   
}
